/*
 * 版权所有 (C) 2013 长沙市红耀通信息技术有限公司。保留所有权利。
 *
 * http://www.zhiqim.com/fadfox/ 欢迎加盟[知凡狐]兴趣小组。
 *
 * 创建说明： 2013-6-29 由 zhichenggang创建
 */
package net.redxyz.repost.resource.action.plan;

import java.util.List;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.repost.resource.model.SellerCatRoot;
import net.redxyz.repost.resource.presenter.RepostPresenter;
import net.redxyz.repost.service.Main;
import net.redxyz.repost.service.dao.SysParamDao;
import net.redxyz.top.TaobaoService;
import net.redxyz.top.data.item.Item;
import net.redxyz.top.repost.dbo.AssRepostPlan;
import net.redxyz.top.repost.dbo.AssRepostPlanDetail;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.GetPostAction;
import org.zhiqim.kernel.Global;
import org.zhiqim.kernel.util.DateTimes;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 均衡上下架计划调整
 *
 * @version v7.0.1 @author zouzhigang 2021-7-29 新建与整理
 */
public class RepostPlanAdjustAction extends GetPostAction
{
    @Override
    protected void validate(HttpRequest request)
    {
    }
    
    public void doGet(HttpRequest request) throws Exception
    {// Add
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        SellerCatRoot seRoot = sessionUser.getSeRoot();

        String planId = request.getParameter("planId");
        AssRepostPlan plan = ORM.table().item(AssRepostPlan.class, planId);
        String sellerCids = plan.getPlanSellerCids();

        String itemScript = RepostPresenter.getItemScriptStrByModify(seRoot, sellerCids);

        request.setAttribute("itemScript", itemScript);
        request.setAttribute("plan", plan);
    }

    public void doPost(HttpRequest request) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);

        String planId = request.getParameterOnCNT("planId");
        String planName = request.getParameterOnCNT("planName");
        String planWeeks = request.getParameterOnCNT("planWeeks");
        String planHours = request.getParameterOnCNT("planHours");
        String planTitle = request.getParameterOnCNT("planTitle");
        int isInclude = request.getParameterIntOnCNT("isInclude");
        String planSellerCids = request.getParameterOnCNT("planSellerCids");

        List<Item> thisItemList = TaobaoService.getItemService().getOnsaleItemListAll(sessionUser.getNick(), sessionUser.getSessionId(), planSellerCids,
            planTitle);

        int maxItemNum = SysParamDao.getInt("limit.param", "repostMaxItemNum", 2000);
        if (thisItemList.size() > maxItemNum)
        {
            request.returnHistory("创建上架计划最多支持" + maxItemNum + "个宝贝，请重新选择类目操作");
            return;
        }

        AssRepostPlan item = ORM.table().item(AssRepostPlan.class, planId);

        AssRepostPlan plan = new AssRepostPlan();
        plan.setPlanName(planName);
        plan.setPlanWeeks(planWeeks);
        plan.setPlanHours(planHours);
        plan.setPlanSellerCids(item.getPlanSellerCids());
        plan.setPlanTitle(planTitle);

        String time = DateTimes.getDateTimeString();

        plan.setNick(sessionUser.getNick());
        plan.setPlanId(item.getPlanId());
        plan.setCreateTime(item.getCreateTime());
        plan.setUpdateTime(time);
        plan.setPlanStatus(0);
        plan.setPlanOperateStatus(0);
        plan.setPlanItemNum(0);
        plan.setPlanSequence(0);

        if (isInclude == 1)
            plan.setPlanDownIids(item.getPlanDownIids());

        Selector selector = new Selector();
        selector.addMaybe("planId", planId);
        selector.addReplace("d", "001");
        ORM.table().delete(AssRepostPlanDetail.class, selector);

        ORM.table().delete(AssRepostPlan.class, planId);
        ORM.table().insert(plan);

        Global.getService(Main.class).postRequest(plan);

        request.setRedirect("/repost.htm");
    }
}
